﻿







#region Designer generated code
using System;

using TrueCode.Core;
using TrueCode.Core.Aspects;
using TrueCode.Core.EditableEntity;
using TrueCode.Core.Tests.EditableEntity;

namespace TrueCode.Core.Tests.EditableEntity
{
	
	public partial class EditableEntityCompilationTest : IEditableEntity< Entity >
	{
		private EditableObjectState _state;
    private Entity _source;
		
				
		private EditableObjectState _stateBeforeDeleted;

					private System.String _originalTitle;
  
    			private System.String _Title;
  			private System.String _Name;
  	
		#region .ctors
          protected
    EditableEntityCompilationTest( Entity source, bool isNew = false)
		{
			if( source == null ) throw new ArgumentNullException( "source" );
			_source = source;

			GetValues(source);

      if(isNew)
  			_state = EditableObjectState.New;
      else
      {
        StoreOriginalValues();
        _state = EditableObjectState.Original;
      }
		}
		
          protected
    EditableEntityCompilationTest( EditableEntityCompilationTest source, bool isNew = false )
		{
      if( source == null ) throw new ArgumentNullException( "source" );

      GetValues(source);

      if(isNew)
  			_state = EditableObjectState.New;
      else
      {
        StoreOriginalValues();
        _state = EditableObjectState.Original;
      }
		}

               protected
    EditableEntityCompilationTest( System.String Name,System.String Title )
     {
                _Name = Name;
                _Title = Title;
                _state = EditableObjectState.New;
     }
		#endregion
	
		#region Entity Properties
    			
			public System.String Name
			{
				get{ return _Name; }
			}
		
					
			public System.String Title
			{
				get{ return _Title; }
				set
				{
					_Title = value;
					SetAsModified();
					PropertyChanged.Raise( this, () => Title );
				}
			}
			
				#endregion

		#region Implementation of IEditableEntity
    [CanBeNull]
		public Entity Entity
		{
			get{ return _source; }
		}
		
		public EditableObjectState State
		{
			get { return _state; }
			private set
			{
				if( _state != value )
				{
					_state = value;
					PropertyChanged.Raise( this, () => State );
				}
			}
		}
		
		public void AcceptChanges()
		{
			if( _state == EditableObjectState.Deleted )
				throw new InvalidOperationException( "Can not accept changes of the deleted entity " );
				
			if( _state != EditableObjectState.Original )
			{
				StoreOriginalValues();
        State = EditableObjectState.Original;
        ChangesAccepted.Raise(this);
			}
		}

    public event EventHandler< EditableEntityCompilationTest, EventArgs > ChangesAccepted;
    public event EventHandler< EditableEntityCompilationTest, EventArgs > ChangesRejected;
		
		public void RejectChanges()
		{
			if( _state == EditableObjectState.New )
				throw new InvalidOperationException( "Can not reject changes of the New entity " );

      if( State != EditableObjectState.Original)
      {
			  if( State == EditableObjectState.Deleted )
				  State = _stateBeforeDeleted;
			  else
			  {
				  					  					  _Title = _originalTitle;
				  				
          State = EditableObjectState.Original;
				  PropertyChanged.Raise( this, null );
			  }
        ChangesRejected.Raise(this);
      }
		}
		
		public void MarkDeleted()
		{
			if( _state == EditableObjectState.New )
				throw new InvalidOperationException( "Can not mark New entity as Deleted" );

      if( _state != EditableObjectState.Deleted )
			{
        // If RejectChanges will be called, we should reject MarkDeleted change
			  _stateBeforeDeleted = State;
			  State = EditableObjectState.Deleted;
			  OnMarkedDeleted();
      }
		}
		
    public void SetValuesTo(Entity entity)
    {
      			  entity.Title = _Title;
			    }

    public void SetValuesTo(EditableEntityCompilationTest editable)
    {
      			  editable.Title = _Title;
			    }

    [Obsolete]
    public void UpdateAll( Entity entity )
    {
      
    }
		#endregion
		
		#region Protected
		protected virtual void OnMarkedDeleted(){}
		#endregion

    	#region Private	
		private void GetValues(Entity entity)
		{
							_Name = entity.Name; 				_Title = entity.Title; 		}

    private void GetValues(EditableEntityCompilationTest editable)
		{
							_Title = editable.Title;
     
     				_Name = editable.Name; 		}

    private void StoreOriginalValues()
    {
              _originalTitle = _Title;
         }
		#endregion
	
    private void SetAsModified()
		{
			if( State == EditableObjectState.Deleted )
				throw new InvalidOperationException( "Can not modify deleted entity, use RejectChanges to undelete it" );

			if( State != EditableObjectState.New )
				State = EditableObjectState.Modified;
		}

		#region Implementation of INotifyPropertyChanged
		public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

		protected System.ComponentModel.PropertyChangedEventHandler PropertyChangedEvent
		{
		get{ return PropertyChanged; }
		}
		#endregion
	}
	
}

#endregion